<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" style="border-right: #a2a2a2 solid 1px;overflow: hidden">
        <div class="head-container" style="display:flex;text-align: center;margin-bottom: 10px;border-bottom: #a2a2a2 solid 1px">
         <div style="margin-left: 30px;margin-right: 40px;overflow: hidden">园区列表</div>
          <el-button size="mini" style="margin-bottom: 10px;height: 30px;width: 60px;" @click="resetlist" type="info" plain>重置</el-button>
        </div>
        <div class="head-container">
          <el-tree :data="managementList" :props="defaultProps" :expand-on-click-node="false"
                   :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24" >
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="78px"
        >
          <el-form-item label="企业名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="负责人" prop="legalPerson">
            <el-input v-model="queryParams.legalPerson" placeholder="请输入负责人" clearable
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                       v-hasPermi="['village:enterprise:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                       v-hasPermi="['village:enterprise:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                       v-hasPermi="['village:enterprise:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                       v-hasPermi="['village:enterprise:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="enterpriseList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="编号" align="center">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="企业名称" align="center" prop="name"/>
          <el-table-column label="负责人" align="center" prop="legalPerson"/>
          <el-table-column label="电话" align="center" prop="tel"/>
          <el-table-column label="企业地址" align="center" prop="addr"/>
          <el-table-column label="邮编" align="center" prop="postcode"/>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="skipDetail(scope.row)"
                         v-hasPermi="['village:pedagogical:query']"
              >详情
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['village:enterprise:edit']"
              >修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['village:enterprise:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改园区企业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="720px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" inline>

        <el-form-item label="园区列表" prop="villageGardenManagementId">
          <el-select style="width: 202px" v-model="form.villageGardenManagementId" clearable filterable placeholder="请选择">
            <el-option v-for="dict in options" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称"/>
        </el-form-item>
        <el-form-item label="负责人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入负责人"/>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话"/>
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份"/>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市"/>
        </el-form-item>

        <el-form-item label="邮编" prop="postcode">
          <el-input v-model="form.postcode" placeholder="请输入邮编"/>
        </el-form-item>
        <el-form-item label="企业地址" prop="addr">
          <el-input v-model="form.addr" style="width: 514px" placeholder="请输入企业地址"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" style="width: 514px" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="detailopen" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" inline>
        <el-form-item style="width: 300px;" label="园区列表：" >
          {{getname}}
        </el-form-item>
        <el-form-item label="企业名称：" >
          {{form.name}}
        </el-form-item>
        <el-form-item style="width: 300px;" label="负责人：" >
          {{form.legalPerson}}
        </el-form-item>
        <el-form-item label="电话：" prop="tel">
          {{form.tel}}
        </el-form-item>
        <el-form-item style="width: 300px;" label="省份：" >
          {{form.province}}
        </el-form-item>
        <el-form-item label="城市：" prop="city">
          {{form.city}}
        </el-form-item>

        <el-form-item style="width: 500px;" label="邮编：" >
          {{form.postcode}}
        </el-form-item>
        <el-form-item style="width: 500px;" label="企业地址：" >
          {{form.addr}}
        </el-form-item>
        <el-form-item label="备注：" style="width: 700px" >
          <div style="width: 540px;">
            {{ form.remark }}
          </div>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import {
  listEnterprise,
  getEnterprise,
  delEnterprise,
  addEnterprise,
  updateEnterprise
} from '@/api/village/enterprise'
import { noListManagement} from "@/api/village/management";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getTopics } from '@/api/village/topics'
import item from '@/layout/components/Sidebar/Item.vue'

export default {
  name: 'Enterprise',
  components: {
    Treeselect
  },
  data() {
    return {
      detailopen:false,
      options: [],
      defaultProps: {
        children: 'children',
        label: 'parkName'
      },
      managementOptions:[],
      managementList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 园区企业信息表格数据
      enterpriseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        legalPerson: null,
        tel: null,
        province: null,
        city: null,
        addr: null,
        postcode: null,
        villageGardenManagementId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '负责人电话不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}|^(\+\d{1,4})?[-\s]?(\(?\d{1,4}\)?[-\s]?)?\d{1,4}[-\s]?\d{1,4}([-\s]?\d{1,9})?$/,
            message: '请输入正确的电话号码（支持手机号和座机）',
            trigger: 'blur'
          }
        ],
        addr: [
          { required: true, message: '企业地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.gettreeList()

  },
  computed: {
    // 使用计算属性
    getname() {
      let namelist = this.options.filter(item=>item.value === this.form.villageGardenManagementId)

      if (namelist !== [] && namelist.length > 0){

        let name = namelist[0]
        return name.label;
      }

      // console.log(name)
      return '';
    }
  },
  methods: {

    resetlist(){
      this.queryParams.villageGardenManagementId = null
      this.getList()
    },
    gettreeList() {
      this.loading = true;
      noListManagement({}).then(response => {
        this.managementList = this.handleTree(response.rows, "id", "parentId");

        this.loading = false;
      });
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    handleNodeClick(data) {
      if (data.id === 1){
        this.queryParams.villageGardenManagementId = null
        this.getList()
        return
      }
      console.log(data)
      this.queryParams.villageGardenManagementId = data.id
      this.getList()
    },
    skipDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getEnterprise(id).then(response => {
        this.form = response.data
        this.detailopen = true

      })
    },
    /** 查询园区企业信息列表 */
    getList() {
      this.loading = true
      listEnterprise(this.queryParams).then(response => {
        this.enterpriseList = response.rows
        this.total = response.total
        this.loading = false
      })
      noListManagement({}).then(response => {
        this.options = response.rows.map(item => ({
          label: item.parkName,
          value: item.id
        }))

        this.options = this.options.filter(item=>item.value !== 1)

      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        legalPerson: null,
        tel: null,
        province: null,
        city: null,
        addr: null,
        postcode: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        villageGardenManagementId:null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加园区企业信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEnterprise(id).then(response => {
        this.form = response.data
        console.log(this.form)
        this.open = true
        this.title = '修改园区企业信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {

        if (valid) {
          if (this.form.id != null) {
            updateEnterprise(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEnterprise(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除园区企业名称为"' + row.name + '"的数据项？').then(function() {
        return delEnterprise(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('village/enterprise/export', {
        ...this.queryParams
      }, `enterprise_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
